import http from '../../apis/api.js';
import util from '../../utils/util.js';
const app = getApp();
let self;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pagination: {
      page: 1,
      limit:8,
      more: 0
    },
    loading: false,
    orderList:[],
    custNo:null,
    imageurl: '/images/img1.png'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.orderList = [];
 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (!self.data.custNo){
      wx.getStorage({
        key: 'loginInfo',
        success: function (res) {
          if (res.data.id) {
            self.data.pagination.page=1;
            self.data.custNo = res.data.custNo;
            self.data.orderList = [];
            self.getOrderList();
          }
        },
        fail(){
          self.data.pagination.page = 1;
          self.data.orderList = [];
          self.getOrderList();
        }
      })
    }else{
      self.data.pagination.page = 1;
      self.data.orderList = [];
      self.getOrderList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (self.data.pagination.more > 0) {
      self.data.pagination.page++;
      self.getOrderList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  getOrderList(){/**获取订单列表 */
    let postData = {
      custNo: self.data.custNo,
      limit: self.data.pagination.limit,
      page: self.data.pagination.page
    };
    self.setData({
      loading: true
    })
    http.orderList(postData).then(res => {
      if (res.code == 0) {
        if (res.page.records.length>0){
          res.page.records.forEach((el,i)=>{
            el.statusObj = util.getOrderStatus(el.orderStat)[0];
            console.log(util.formatTime(el.instTime, 'YYYY-MM-DD hh:mm:ss'))
            el.formatTime = el.statusObj.label == "已支付" ? util.formatTime(el.payTime, 'YYYY-MM-DD hh:mm:ss')  :util.formatTime(el.instTime, 'YYYY-MM-DD hh:mm:ss');
            if (el.prodType =='50400101'){
              el.storePics.forEach(item => {
                if (item.picType == '51000101') {
                  el.thumbnail = item.inPath;
                }
              })
            }else{
              el.thumbnail = self.data.imageurl;
            }
           
            self.data.orderList.push(el);
          })
          
          self.data.pagination.more = Number(res.page.current) * Number(res.page.size) < res.page.total ? 1 : 0;
        }
        console.log(self.data.pagination.more);
        self.setData({
          orderList: self.data.orderList,
          pagination: self.data.pagination,
          loading: false
        })
      }
    })
  },
  goPay(e){/**点击支付按钮 */
    let orderNo=e.target.dataset.id;
    http.getWxOrder({ orderNo}).then(result=>{
      let wxParams = {
        timeStamp: result.data.wxTimeStamp,
        nonceStr: result.data.wxNonceStr,
        package: result.data.wxPayPackage,
        signType: result.data.wxSignType,
        paySign: result.data.wxPaySign,
      }
      util.wxPay(wxParams).then(res => {
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        })
      },error=>{
        wx.showToast({
          title: '支付失败',
          icon: 'none',
          duration: 2000
        })
      })
    })
  }
})